home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 287_01 / circle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-23  |  3.3 KB  |  85 lines

  1. #include <stdio.h>
  2. #include <gds.h>
  3.  
  4. struct cpoint {
  5.     int point, state, region;
  6. };
  7.  
  8. /*===========================================================*
  9.  *                                                           *
  10.  *  This file contains 2 external usable routines to draw    *
  11.  *  circle and arc.                                          *
  12.  *      Circle(centerx,centery,radius)                       *
  13.  *          It draws a logical circle and the 'circle' will  *
  14.  *          look like a circle only on a screen with square  *
  15.  *          pixels (width and height is the same.            *
  16.  *                                                           *
  17.  *      Arc1(centerx,centery,radius,spec)                    *
  18.  *          Draw n/8 of a circle. n can be 0 to 8. Read      *
  19.  *          user's manual on the use of the function.        *
  20.  *                                                           *
  21.  *===========================================================*/
  22.  
  23. /*-----------------------------------------------------------*
  24.  *  The algorithm used in drawing circle is come from the    *
  25.  *  the book "Fundamentals of interactive computer Graphics" *
  26.  *  by Foley[1982]. Read the book about the algorithm.       *
  27.  *                                                           *
  28.  *  The points drawn by this method will have less than or   *
  29.  *  equal to half of a pixel from the corresponding point    *
  30.  *  on a perfect circle of the same size.                    *
  31.  *-----------------------------------------------------------*/
  32.  
  33. Circle(centerx,centery,radius)
  34. int centerx,centery,radius;
  35. {
  36.     register int tmp1,tmp2;
  37.     struct cpoint dummy1,dummy2;
  38.  
  39.     if (radius <= 0) return;
  40.     centerx += ORGX;  /* get absolute position */
  41.     centery += ORGY;
  42.  
  43.     /* return if the circle must be outside the window */
  44.     if ((centerx+radius < WINX1) || (centerx-radius > WINX2) ||
  45.         (centery+radius < WINY1) || (centery-radius > WINY2)) return;
  46.  
  47.     tmp1=centerx+radius; tmp2=centery+radius;
  48.     if (outside(tmp1,tmp2)) {
  49.         setcptr(1);  /* use '1' if part of the circle may be outside
  50.                         the window */
  51.     } else {
  52.         tmp1=centerx-radius; tmp2=centery-radius;
  53.         if (outside(tmp1,tmp2))
  54.             setcptr(1);  /* use '1' if part of the circle may be outside
  55.                         the window */
  56.         else  /* the whole circle is within the window */
  57.             setcptr(0);
  58.     }
  59.     ARCSPEC=0xff;    /* draw the whole circle */
  60.     dummy1.point=dummy2.point=radius;
  61.     /* always use solid line */
  62.     circ(centerx,centery,radius,0xffff,&dummy1,&dummy2);
  63. }
  64.  
  65. Arc1(centerx,centery,radius,spec)
  66. int centerx,centery,radius,spec;
  67. {
  68.     struct cpoint dummy;
  69.  
  70.     if (radius <= 0) return;
  71.     centerx += ORGX;   /* get absolute location */
  72.     centery += ORGY;
  73.  
  74.     /* if the whole circle is outside is outside the window, return */
  75.     if ((centerx+radius < WINX1) || (centerx-radius > WINX2) ||
  76.         (centery+radius < WINY1) || (centery-radius > WINY2)) return;
  77.     setcptr(1);  /* always use '1' in arc drawing */
  78.     ARCSPEC=spec;
  79.     dummy.point=32767;
  80.     dummy.region=0x8000;
  81.     /* use the STYLE setting */
  82.     circ(centerx,centery,radius,STYLE,&dummy,&dummy);
  83. }
  84.  
  85.